package volkov.ja.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import ru.yandex.qatools.htmlelements.loader.HtmlElementLoader;

public class InternalPage extends AnyPage {

	private static By pageReadyBy = By.cssSelector("nav");

	// need to define @Block's @FindBy here
	@FindBy(css = "header nav")
	private NavigationBlock navigation;
	
	public InternalPage(PageManager pages) {
		super(pages);
	}

	public InternalPage ensurePageLoaded() {
		super.ensurePageLoaded();
		pages.getWait().until(presenceOfElementLocated(pageReadyBy));
		return this;
	}

	public UserProfilePage clickUserProfilePage() {
		navigation.goProfile();
		return pages.userProfilePage;
	}

	public UserManagementPage clickUserManagementLink() {
		navigation.goUserManagement();
		return pages.userManagementPage;
	}

	public MovieListPage clickMovieListLink() {
		navigation.goHome();
		return pages.movieListPage;
	}

	public LoginPage clickLogoutLink() {
		navigation.logout();
		// TODO: move to NavigationBlock
		pages.getWait().until(alertIsPresent()).accept();
		return pages.loginPage;
	}

	public boolean pageReady() {
		try {
			navigation.isDisplayed();
		} catch (NoSuchElementException e) {
			return false;
		}
		return true;
	}

}
